package com.iptv.grape.ui.frgmt;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;

import com.bumptech.glide.Glide;
import com.iptv.grape.R;
import com.iptv.grape.bu.AppConfig;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.bu.imp.QuitFullScreen;
import com.iptv.grape.net.Entity.RecommendData;
import com.iptv.grape.ui.BaseFrgmt;
import com.iptv.grape.ui.control.task.RecommendTask;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.view.AlwaysMarqueeTextView2;

import java.util.ArrayList;
import java.util.List;

// 直播底部推荐
public class RecommendFragment extends BaseFrgmt {

    private RelativeLayout metroview0;

    private List<AlwaysMarqueeTextView2> titles = new ArrayList<AlwaysMarqueeTextView2>();
    private List<ImageView> imgBottoms = new ArrayList<ImageView>();
    private List<RelativeLayout> layouts = new ArrayList<>();

    @Override
    protected int layoutId() {
        if(AppConstant.isTvStb()) {
            return R.layout.recomm_tv_fragment;
        }
        else {
            return R.layout.recomm_fragment;
        }
    }

    @Override
    protected void initView(@NonNull View root, @Nullable Bundle savedInstanceState) {

        metroview0 = root.findViewById(R.id.metroview0);
        metroview0.setFocusable(true);
        metroview0.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                //获取焦点时变化
                if (hasFocus) {
                    metroview0.setBackgroundResource(R.drawable.button_back_fucs);
                    ViewCompat.animate(v)
                            .scaleX(1.2f)
                            .scaleY(1.2f)
                            .start();
                } else {
                    metroview0.setBackgroundResource(R.drawable.button_back_blus);
                    ViewCompat.animate(v)
                            .scaleX(1)
                            .scaleY(1)
                            .start();
                }
            }
        });
        metroview0.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                QuitFullScreen.quitFullScreen(RecommendFragment.this.mActivity);
            }
        });
//        com.iptv.grape.view.RoundAngleImageView
        imgBottoms.add((ImageView)root.findViewById(R.id.img_bottom1));
        imgBottoms.add((ImageView)root.findViewById(R.id.img_bottom2));
        imgBottoms.add((ImageView)root.findViewById(R.id.img_bottom3));
        imgBottoms.add((ImageView)root.findViewById(R.id.img_bottom4));
        imgBottoms.add((ImageView)root.findViewById(R.id.img_bottom5));
        imgBottoms.add((ImageView)root.findViewById(R.id.img_bottom6));

        titles.add((AlwaysMarqueeTextView2)root.findViewById(R.id.title_bottom1));
        titles.add((AlwaysMarqueeTextView2)root.findViewById(R.id.title_bottom2));
        titles.add((AlwaysMarqueeTextView2)root.findViewById(R.id.title_bottom3));
        titles.add((AlwaysMarqueeTextView2)root.findViewById(R.id.title_bottom4));
        titles.add((AlwaysMarqueeTextView2)root.findViewById(R.id.title_bottom5));
        titles.add((AlwaysMarqueeTextView2)root.findViewById(R.id.title_bottom6));

        layouts.add((RelativeLayout)root.findViewById(R.id.metroview1));
        layouts.add((RelativeLayout)root.findViewById(R.id.metroview2));
        layouts.add((RelativeLayout)root.findViewById(R.id.metroview3));
        layouts.add((RelativeLayout)root.findViewById(R.id.metroview4));
        layouts.add((RelativeLayout)root.findViewById(R.id.metroview5));
        layouts.add((RelativeLayout)root.findViewById(R.id.metroview6));

        for(int i = 0; i < layouts.size(); i++){
            RelativeLayout layout = layouts.get(i);
            //layout.setTag(titles.get(i));
            layout.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    Object obj = view.getTag();
                    if(obj != null && obj instanceof RecommendData){

                        RecommendData data = (RecommendData)obj;
                        AppLog.getCurLog().out(data.getAssetID());
                        GrapeContext.curGrapeContext().getCurMediaPlayer().stop();
                        GrapeContext.curGrapeContext().setFullScreen(false);
                        GrapeContext.curGrapeContext().setFocus(false);
                        GrapeContext.curGrapeContext().setShowRecommendUI(false);
                        String url= AppConfig.getAppConfig().getEpgDetailUrl() +data.getAssetID()+"/new";
                        GrapeContext.curGrapeContext().removeVodFragment(RecommendFragment.this.mActivity);
                        RecommendFragment.this.mActivity.startActivityForResult(new Intent(RecommendFragment.this.mActivity, com.iptv.grape.DetailActivity.class).putExtra("recommendUrl",url),155);
                        GrapeContext.curGrapeContext().closeHandle();
                        //AppLog.getCurLog().out("跳转Act");
                    }
                }
            });
            // 设置焦点变化的样式
            layout.setOnFocusChangeListener(new OnFocusListener(titles,i) {
                @Override
                public void onFocusChange(View v, boolean hasFocus,AlwaysMarqueeTextView2 textVeiw) {
                    textVeiw.setIsFocused(true);
                    //获取焦点时变化
                    if (hasFocus) {
                        metroview0.setBackgroundResource(R.drawable.img_back_fucs);
                        ViewCompat.animate(v)
                                .scaleX(1.10f)
                                .scaleY(1.10f)
                                .start();

                    } else {
                        metroview0.setBackgroundResource(R.drawable.img_back_blus);
                        ViewCompat.animate(v)
                                .scaleX(1)
                                .scaleY(1)
                                .start();
                    }
                }
            });
        }

        //metroview0.requestFocus();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                metroview0.requestFocus();
                //AppLog.getCurLog().out("metroview0.requestFocus()");
            }
        }, AppConfig.getAppConfig().getDelayedTimeVal());
        new RecommendTask().execute(this);
    }

    public void initUI(List<RecommendData> result){

        if(result != null && result.size() > 0){
            for(int i = 0; i < result.size(); i++){
                RecommendData data = result.get(i);
                if(i < titles.size()) {
                    titles.get(i).setText(data.getName());
                    Glide.with(this.mActivity).load(data.getPoster()).into(imgBottoms.get(i));
                    layouts.get(i).setTag(data);
                    //imgBottoms.get(i).setTag(data);
                }
                else {
                    break;
                }
            }
        }
    }
    private abstract class OnFocusListener implements View.OnFocusChangeListener {
        private List<AlwaysMarqueeTextView2>textViews;
        private int item;
        public OnFocusListener(List<AlwaysMarqueeTextView2> textViews,int item){
            this.textViews =textViews;
            this.item= item;
        }

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            int size = textViews.size();
            for(int i=0;i<size;i++){
                if(i!=item)
                    textViews.get(i).setIsFocused(false);
            }
            AlwaysMarqueeTextView2 textView = textViews.get(item);
            onFocusChange(v,hasFocus,textView);
        }
        public abstract void onFocusChange(View v, boolean hasFocus,AlwaysMarqueeTextView2 textView);
    }
    // 销毁
    public void onDestroy() {
        super.onDestroy();

    }

    // 界面隐藏显示回调函数
    @Override
    public void onHiddenChanged(boolean hidden){
        super.onHiddenChanged(hidden);
        // 不在最前端界面显示
        if (hidden) {
            // 隐藏
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    metroview0.requestFocus();
                }
            },  AppConfig.getAppConfig().getDelayedTimeVal());
            new RecommendTask().execute(this);
        }
    }


    public static RecommendFragment newInstance() {
        RecommendFragment frag = new RecommendFragment();
        Bundle bundle = new Bundle();
        //bundle.putString("dateTimeString", dateTimeString);
        frag.setArguments(bundle);   //设置参数
        return frag;
    }

}
